package com.zego.wawaji.ui.dialog;

import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.bean.Updata;
import com.zego.wawaji.base.config.AreaConfig;
import com.zego.wawaji.base.log.PToast;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.ui.bean.pay.Pay;
import com.zego.wawaji.ui.center.wawa.WawaExchangeDialog;
import com.zego.wawaji.ui.dialog.address.AddressPicker;
import com.zego.wawaji.ui.dialog.game.StartGameDialog;
import com.zego.wawaji.ui.dialog.invite.InviteCodeDialog;
import com.zego.wawaji.ui.dialog.pay.PayBottomDialog;
import com.zego.wawaji.ui.dialog.pay.PayDialog;
import com.zego.wawaji.ui.dialog.share.InviteDialog;
import com.zego.wawaji.ui.dialog.share.ShareDialog;
import com.zego.wawaji.ui.dialog.share.ShareRedDialog;
import com.zego.wawaji.ui.dialog.updata.UpdateDialog;

import java.util.Map;

/**
 * Dialog工具类
 */
public class PickerDialogUtil {

    /**
     * 显示地区选择框Dialog  有不限选项
     *
     * @param activity
     * @param listener
     */
    public static void showAddressPickerDialog1(FragmentActivity activity, AddressPicker.OnAddressPickListener listener, String proDefalutValue, String cityDefaultValue) {
        showAddressPickerDialog(activity, "居住地", listener, proDefalutValue, cityDefaultValue, true);
    }

    /**
     * 显示地区选择框Dialog 移除不限选项
     *
     * @param activity
     * @param listener
     */
    public static void showAddressPickerDialog2(FragmentActivity activity, AddressPicker.OnAddressPickListener listener, String proDefalutValue, String cityDefaultValue) {
        showAddressPickerDialog(activity, "居住地", listener, proDefalutValue, cityDefaultValue, false);
    }

    /**
     * 显示地区选择框Dialog
     *
     * @param activity
     * @param title
     * @param listener
     * @param limit    true : 有不限选项.  false : 移除不限选项
     */
    public static void showAddressPickerDialog(FragmentActivity activity, CharSequence title, AddressPicker.OnAddressPickListener listener, String proDefalutValue, String cityDefaultValue, boolean limit) {
        AddressPicker addressPicker;
        if (limit) {
            addressPicker = new AddressPicker(activity, AreaConfig.getInstance().getLimitProvince());
        } else {
            addressPicker = new AddressPicker(activity, AreaConfig.getInstance().getCommonProvince());
        }
        addressPicker.setTitleText(title);
        addressPicker.setSelectedItem(proDefalutValue, cityDefaultValue);
        addressPicker.setOnAddressPickListener(listener);
        addressPicker.show();
    }

    /**
     * 文字提示Dialog
     *
     * @param activity
     * @param listener 取消/确定按钮回调
     * @param content  对话框提示内容
     * @param title    对话框标题
     * @param isBoth   是否双按钮显示，true : 双按钮
     */
    public static void showSimpleTipDialog(FragmentActivity activity, SimpleTipDialog.ConfirmListener listener, CharSequence content, CharSequence title, boolean isBoth) {
        showSimpleTipDialog(activity, listener, content, title, "取消", "确定", isBoth);
    }

    /**
     * 文字提示Dialog，可设置是否点击返回键消失，其他参数说明见{@link #showSimpleTipDialog}
     *
     * @param isCancelable false:不消失
     */
    public static void showTipDialogCancelBack(FragmentActivity activity, SimpleTipDialog.ConfirmListener listener, CharSequence content, CharSequence title, String cancelTxt, String submitTxt, boolean isBoth, boolean isCancelable) {
        SimpleTipDialog tipDialog = new SimpleTipDialog(activity);
        tipDialog.setTitleText(title);
        tipDialog.setContentString(content);
        tipDialog.setCancelText(cancelTxt);
        tipDialog.setSubmitText(submitTxt);
        tipDialog.setCancelVisible(isBoth);
        tipDialog.setConfirmListener(listener);
        tipDialog.setDialogCancelable(isCancelable);
        tipDialog.show();
    }

    /**
     * 显示普通提示dialog
     *
     * @param activity  FragmentActivity实例
     * @param listener  按钮点击监听
     * @param content   提示文字内容
     * @param title     dialog标题
     * @param cancelTxt 取消按钮文字
     * @param submitTxt 确定按钮文字
     * @param isBoth    是否显示两个按钮，如果为false，则默认值显示确认按钮
     */
    public static void showSimpleTipDialog(FragmentActivity activity, SimpleTipDialog.ConfirmListener listener, CharSequence content, CharSequence title, String cancelTxt, String submitTxt, boolean isBoth) {
        SimpleTipDialog tipDialog = new SimpleTipDialog(activity);
        tipDialog.setTitleText(title);
        tipDialog.setContentString(content);
        tipDialog.setCancelText(cancelTxt);
        tipDialog.setSubmitText(submitTxt);
        tipDialog.setCancelVisible(isBoth);
        tipDialog.setConfirmListener(listener);
        tipDialog.show();
    }

    /**
     * 显示普通提示dialog
     *
     * @param activity  FragmentActivity实例
     * @param listener  按钮点击监听
     * @param content   提示文字内容
     * @param title     dialog标题
     * @param cancelTxt 取消按钮文字
     * @param submitTxt 确定按钮文字
     * @param isBoth    是否显示两个按钮，如果为false，则默认值显示确认按钮
     * @param color     文字内容颜色
     */
    public static void showSimpleTipDialogExt(FragmentActivity activity, SimpleTipDialog.ConfirmListener listener, CharSequence content, CharSequence title, String cancelTxt, String submitTxt, boolean isBoth, int color) {
        SimpleTipDialog tipDialog = new SimpleTipDialog(activity);
        tipDialog.setTitleText(title);
        tipDialog.setContentString(content);
        tipDialog.setContentColor(color);
        tipDialog.setCancelText(cancelTxt);
        tipDialog.setSubmitText(submitTxt);
        tipDialog.setCancelVisible(isBoth);
        tipDialog.setConfirmListener(listener);
        tipDialog.show();
    }

    /**
     * 简单提示对话框（退出，删除照片等）
     */
    public static void showSimpleAlertDialog(FragmentActivity activity, SimpleTipDialog.ConfirmListener listener, CharSequence content, CharSequence title) {
        showSimpleAlertDialog(activity, listener, content, R.color.txt_color1, title);
    }

    /**
     * 简单提示对话框,可自定义提示文字颜色
     */
    public static void showSimpleAlertDialog(FragmentActivity activity, SimpleTipDialog.ConfirmListener listener, CharSequence content, int color, CharSequence title) {
        SimpleTipDialog tipDialog = new SimpleTipDialog(activity);
        tipDialog.setTitleText(title);
        tipDialog.setContentString(content);
        tipDialog.setContentColor(color);
        tipDialog.setConfirmListener(listener);
        tipDialog.show();
    }

    /**
     * 展示签到弹框
     */
    public static void showSignDialog(FragmentActivity activity, int awardNum, int dayNum) {
        SignDialog signDialog = new SignDialog(activity);
        signDialog.setAwardNum(awardNum);
        signDialog.setDayNum(dayNum);
        signDialog.show();
    }

    /**
     * 展示升级弹框
     */
    public static void showUpdateDialog(FragmentActivity activity, Updata appUpdate) {
        UpdateDialog updateDialog = new UpdateDialog();
        updateDialog.setData(appUpdate);
        updateDialog.showDialog(activity);
    }

    /**
     * 展示分享弹框
     */
    public static void showShareDialog(FragmentActivity activity) {
        ShareDialog shareDialog = new ShareDialog();
        if (activity instanceof ShareDialog.OnShareClickListener) {
            shareDialog.setOnShareListener((ShareDialog.OnShareClickListener) activity);
        }
        shareDialog.showDialog(activity);
    }

    /**
     * H5分享弹框
     */
    public static void showShareRedDialog(FragmentActivity activity) {
        ShareRedDialog redDialog = new ShareRedDialog();
        redDialog.showDialog(activity);
    }

    /**
     * 娃娃兑换娃娃币成功弹框
     *
     * @param awardNum 奖励娃娃币
     */
    public static void showWawaExchangeDialog(FragmentActivity activity, int awardNum) {
        WawaExchangeDialog dialog = new WawaExchangeDialog(activity);
        dialog.setAwardNum(awardNum);
        dialog.show();
    }

    /**
     * 开始游戏倒计时弹框
     */
    public static void showStartGameDialog(FragmentActivity activity, int rspSeq, String sessionData) {
        StartGameDialog dialog = new StartGameDialog(activity);
        dialog.setData(rspSeq, sessionData);
        dialog.show();
    }

    /**
     * 成功被邀请弹框
     */
    public static void showInviteDialog(FragmentActivity activity, int awardNum) {
        InviteDialog dialog = new InviteDialog(activity);
        dialog.setAwardNum(awardNum);
        dialog.show();
    }

    /**
     * 输入验证码弹框
     */
    public static void showInviteCodeDialog(FragmentActivity activity) {
        InviteCodeDialog dialog = new InviteCodeDialog(activity);
        dialog.show();
    }

    /**
     * 充值方式弹框
     */
    private static PayBottomDialog bottomDialog;
    private static Pay payGoods;

    public static void showPayBottomDialog(Activity activity, final Pay pay) {
        payGoods = pay;
        bottomDialog = new PayBottomDialog(activity, new PayBottomDialog.OnDialogItemClick() {
            @Override
            public void onDialogItemCilck(AdapterView<?> parent, View view, int position, long id, Map<String, Integer> itemInfos) {
                pay(position);
                bottomDialog.dismissDialog();
            }
        });
        bottomDialog.showDialog();
    }

    /**
     * 充值商品列表弹框
     */
    private static PayDialog payDialog;

    public static void showPayDialog(FragmentActivity activity) {
        payDialog = new PayDialog(activity);
        payDialog.show();
    }

    public static void closePayDialog() {
        try {
            if (payDialog != null) {
                payDialog.dismiss();
            }
        } catch (Exception e) {
        }
    }

    private static void pay(int position) {
        if (payGoods == null) {
            PToast.showShort("获取商品信息失败，请稍后重试");
            return;
        }
        if (position == 0) {
            ModuleMgr.getWawaMgr().reqWXPay("金币" + (payGoods.getWwCoinCz() + payGoods.getWwCoinZs()) + "个", payGoods.getId(), null);
        } else if (position == 1) {
            ModuleMgr.getWawaMgr().reqAlipay("金币" + (payGoods.getWwCoinCz() + payGoods.getWwCoinZs()) + "个", payGoods.getId(), null);
        }
    }
}
